package com.yh.service.impl;

import com.yh.config.OtherSource;
import com.yh.dao.UserDao;
import com.yh.entity.User;
import com.yh.service.LoginService;
import com.yh.service.redis.UpdateDataToRedis;
import com.yh.utils.Constants;
import com.yh.utils.Md5Security;
import com.yh.utils.ImgUrl;
import com.yh.vo.LoginVO;
import io.minio.MinioClient;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@DubboService
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UpdateDataToRedis redis;
    @Autowired
    private OtherSource otherSource;
    @Autowired
    private MinioClient minioClient;

    @Override
    public LoginVO userLogin(String userAccount, String passWord) {
        LoginVO vo = login(userAccount, passWord);
        if (vo.getLoginStatus() == Constants.ResponseDataState.RESP_SUCCESS) {
            // 判断用户是否为开发者身份，如果开发者账号以普通用户的身份登录，就将权限降级为普通用户
            boolean isDeveloper = vo.getUser().getUserRole() == Constants.LoginData.DEVELOPER_ROLE;
            // 权限降级
            if (isDeveloper) {
                vo.getUser().setUserRole(Constants.LoginData.USER_ROLE);
            }
            // 将用户信息保存到redis
            redis.saveUserToRedis(vo.getUser());
        }
        return vo;
    }

    @Override
    public LoginVO developerLogin(String userAccount, String passWord) {
        LoginVO vo = login(userAccount, passWord);
        if (vo.getLoginStatus() == Constants.ResponseDataState.RESP_SUCCESS) {
            // 判断用户权限是否正确
            boolean isDeveloper = vo.getUser().getUserRole() == Constants.LoginData.DEVELOPER_ROLE;
            // 如果不是开发者的权限提示账号不存在
            if (!isDeveloper) {
                vo.setLoginStatus(Constants.ResponseDataState.NULL_ACCOUNT);
                return vo;
            }
            // 将用户信息保存到redis
            redis.saveUserToRedis(vo.getUser());
        }
        return vo;
    }

    @Override
    public void signOut(String userId) {
        redis.clearLoginUser(userId);
    }

    /**
     * 登录判断
     */
    private LoginVO login(String userAccount, String passWord) {
        LoginVO loginVO = new LoginVO();
        // 根据账户查询用户信息
        User user = userDao.findUserByAccount(userAccount);
        if (ObjectUtils.isEmpty(user)) {
            loginVO.setLoginStatus(Constants.ResponseDataState.NULL_ACCOUNT);
            return loginVO;
        }
        // 处理头像地址
        user = ImgUrl.userImageHandler(minioClient, otherSource, user);
        // 判断密码是否正确（加密后判断）
        String securityPwd = Md5Security.makePrefixToMd5(user.getPwdPrefix(), passWord);
        boolean isCorrect = user.getSecurityPwd().equals(securityPwd);
        if (!isCorrect) {
            loginVO.setLoginStatus(Constants.ResponseDataState.PASSWORD_ERROR);
            return loginVO;
        }
        loginVO.setLoginStatus(Constants.ResponseDataState.RESP_SUCCESS);
        loginVO.setUser(user);
        return loginVO;
    }
}
